local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CallBackManager = CommonOHOH.load.CallBackManager();

--概述：控制怪物的动画播放
----------
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

	self.player=self.parent.player
	self.character=self.parent
	self.TagCharacter=self.player.character
    self.lastAttackTime= os.clock()

    self.battleCtrl=self.player:GetComponent(_G.CommonOHOH.load.Common_AttackCtrl())

    if self.battleCtrl:GetIsBattle() then
        self:OpenAttack()
    end

    -- self.player.BattleSateEvent:Connect(function(isBattle,battle)
    --     if isBattle then
         
    --         self:OpenAttack()
    --     else
    --         self:CloseAttack()  
    --     end
    -- end)

    
    CallBackManager:AddRbxConnect(self,self.player.BattleSateEvent, function(obj,isBattle,battle)
        if isBattle then
         
            self:OpenAttack()
        else
            self:CloseAttack()  
        end
	end);


    self.IdleState=self.character:GetState("idle")
	self.FollowState=self.character:GetState("follow")
	self.AttackState=self.character:GetState("attack")

    self.State=nil
	-- self.IdleState.EnterEvent:Connect(function()
	-- 	self.State=1
	-- end)

	-- self.FollowState.EnterEvent:Connect(function()
	-- 	self.State=2
	-- end)

end;


function M.dtor(self)
    self:CloseAttack()
	M.super.dtor(self);
end


function M.OpenAttack(self)
    if self.UpdateCon then
        return 
    end
  
	self.UpdateCon=self:AddStepUpdate(function()
        self:Update()
	end)
end


function M.CloseAttack(self)
    if self.UpdateCon then
        self.UpdateCon:Disconnect()
        self.UpdateCon=nil
    end
end

local FirendAttackCD= _G.CommonOHOH.config.Game.FriendAttackCD
--攻击CD
function M.GetAttackCd(self)
    return FirendAttackCD
end


--切换到攻击状态
function M.Attack(self)
    self.character:SwitchState("attack")
end


function M.Update(self)

	if  os.clock()- self.lastAttackTime>self:GetAttackCd() then
        self.lastAttackTime= os.clock()
        self.character:SwitchState("attack")
        self.parent:GetWeapon():StartAttack()
    end

end


return M